//点击修改时执行
function borrow() {
    var url =getProjectPath()+ "/updateUser";
    $.post(url,$("#borrowBook").serialize(), function (response) {
        alert(response.message)
        if (response.success === true) {
            window.location.href = getProjectPath()+"/staff";
        }
    })
}
function addUser() {
    resetFromUser();
    var name=$("#name").val();
    var phone=$("#phone").val();
    var user=$("#user").val();
    var password=$("#password").val();
    var sex=$("#sex").val();
    if (name==null||name==""||phone==null||phone==""||user==null||user==""||password==null||password==""||sex==null||sex=="") {
        alert("员工姓名、性别、电话、账号、密码不能为空");
    }else{
    var url =getProjectPath()+ "/addUser";
    $.post(url,$("#borrowU").serialize(),function (response){
        alert(response.message)
        if (response.success === true) {
            window.location.href = getProjectPath()+"/staff";
        }
    })}
}
function deleteUserById(uid) {
    var url =getProjectPath()+ "/deleteUserById?uid="+uid;
    // var url=getProjectPath()+"/test";
    $.post(url,function (response){
        alert(response.message)
        if (response.success === true) {
            window.location.href = getProjectPath()+"/staff";
        }
    })
}
function deleteByMId(mid){
    var url =getProjectPath()+"/deleteByMId?mid="+mid;
    $.post(url,function (response){
        alert(response.message);
        if (response.success === true) {
            window.location.href = getProjectPath()+"/searchDeleteMd";
        }
    })
}
function findById(uid,doName){
    resetFrom()
    var url =getProjectPath()+"/findById?uid="+uid;
    $.get(url,function (response){
        if (doName==='b'){
            $("#savemsg").attr("disabled",true);
            $("#euid").val(response.data.uid);
            $("#ename").val(response.data.name);
            $("#esex").val(response.data.sex);
            $("#ephone").val(response.data.phone);
            $("#epassword").val(response.data.password);
        }
    })
}
//重置添加和编辑窗口中输入框的内容
function resetFrom() {
    $("#save").attr("disabled",true)
    var $vals=$("#borrowBook input");
    $vals.each(function(){
        $(this).attr("style","").val("")
    });
}
function resetFromUser() {
    $("#asavemsgoe").attr("disabled",true)
    var $vals=$("#borrowU input");
    $vals.each(function(){
        $(this).attr("style","").val("")
    });
}
//检查修改信息的窗口中，修改信息填写是否完整
function checkval(){
    var $inputs=$("#borrowBook input")
    var count=0;
    $inputs.each(function () {
        if($(this).attr("name")=="name"&&($(this).attr("name")=="age")) {
            if ($(this).val() == "" || $(this).val() == "不能为空！") {
                count += 1;
            }
        }
    })
    //如果全部输入框都填写完整，解除确认按钮的禁用状态
    if(count==0){
        $("#savemsg").attr("disabled",false)
    }
}
function checkval1(){
    var $input=$("#borrowU input")
    var count=0;
    $input.each(function () {
        if($(this).attr("name")=="name"&&($(this).attr("name")=="user")) {
            if ($(this).val() == "" || $(this).val() == "不能为空！") {
                count += 1;
            }
        }
    })
    //如果全部输入框都填写完整，解除确认按钮的禁用状态
    if(count==0){
        $("#save").attr("disabled",false)
    }
}
//页面加载完成后，给修改模态窗口的输入框绑定失去焦点和获取焦点事件
$(function() {
    var $inputs=$("#borrowBook input")
    $inputs.each(function () {
        //给输入框绑定失去焦点事件
        $(this).blur(function () {
            if($(this).val()==''){
                $("#savemsg").attr("disabled",true)
                $(this).attr("style","color:red").val("不能为空！")
            }
            else{
                checkval()
            }
        }).focus(function () {
            if($(this).val()=='不能为空！'){
                $(this).attr("style","").val("")
            }else{
                $(this).attr("style","")
            }
        })
    })
});
$(function() {
    var $inputs=$("#borrowU input")
    $inputs.each(function () {
        //给输入框绑定失去焦点事件
        $(this).blur(function () {
            if($(this).val()==''){
                $("#save").attr("disabled",true)
                $(this).attr("style","color:red").val("不能为空！")
            }
            else{
                checkval1()
            }
        }).focus(function () {
            if($(this).val()=='不能为空！'){
                $(this).attr("style","").val("")
            }else{
                $(this).attr("style","")
            }
        })
    })
});
//获取当前项目的名称
function getProjectPath() {
    //获取主机地址之后的目录，如： cloudlibrary/admin/books.jsp
    var pathName = window.document.location.pathname;
    //获取带"/"的项目名，如：/cloudlibrary
    var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
    return  projectName;
}

/**
 * 数据展示页面分页插件的参数
 * cur 当前页
 * total 总页数
 * len   显示多少页数
 * pagesize 1页显示多少条数据
 * gourl 页码变化时 跳转的路径
 * targetId 分页插件作用的id
 */
var pageargs = {
    cur: 1,
    total: 0,
    len: 5,
    pagesize:10,
    gourl:"",
    targetId: 'pagination',
    callback: function (total) {
        var oPages = document.getElementsByClassName('page-index');
        for (var i = 0; i < oPages.length; i++) {
            oPages[i].onclick = function () {
                changePage(this.getAttribute('data-index'), pageargs.pagesize);
            }
        }
        var goPage = document.getElementById('go-search');
        goPage.onclick = function () {
            var index = document.getElementById('yeshu').value;
            if (!index || (+index > total) || (+index < 1)) {
                return;
            }
            changePage(index, pageargs.pagesize);
        }
    }
}
/**
 *药品查询栏的查询参数
 * name 药品名称
 * efficacy 用途
 */
var medicalVO={
    name:'',
    phone:'',
    sex:'',
    password:'',
    efficacy:'',
    cid:'',
    uid:0,
    mid:''
}
//数据展示页面分页插件的页码发送变化时执行
function changePage(pageNo,pageSize) {
    pageargs.cur=pageNo;
    pageargs.pagesize=pageSize;
    document.write("<form action="+pageargs.gourl +" method=post name=form1 style='display:none'>");
    document.write("<input type=hidden name='pageNum' value="+pageargs.cur+" >");
    document.write("<input type=hidden name='pageSize' value="+pageargs.pagesize+" >");
    // 如果跳转的是房间信息查询的相关链接，提交房间查询栏中的参数
    if(pageargs.gourl.indexOf("medical")>=0){
        document.write("<input type=hidden name='name' value="+medicalVO.name+" >");
        document.write("<input type=hidden name='efficacy' value="+medicalVO.efficacy+" >");
    }
    document.write("</form>");
    document.form1.submit();
    pagination(pageargs);
}